/***************************************************************
 * @System            : 生鲜系统
 * @Content           : 继承 SpinEdit，增加相关通用方法或事件
 * @Author            : geping  2017-02-20
 * @Modify            : 
 * @Version           : v1.0
 * @Copyright         : 宁波康铭泰克信息科技有限公司
 ***************************************************************/

using System;
using System.ComponentModel;
using System.Windows.Forms;
using DevExpress.Accessibility;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Drawing;
using DevExpress.XtraEditors.Registrator;
using DevExpress.XtraEditors.Repository;

namespace Comteck.Winforms.Controls {
  /// <summary>
  /// 
  /// </summary>
  [ToolboxItem(true)]
  public partial class MySpinEdit : SpinEdit {

    static MySpinEdit() {
      RepositoryItemMySpinEdit.Register();
    }

    /// <summary>
    /// 
    /// </summary>
    public MySpinEdit() : base() {
      Initialize();
    }

    /// <summary>
    /// 初始化界面
    /// </summary>
    private void Initialize() {
      this.Tag = false;   // 设置全选标记
      //this.EnterMoveNextControl = true;   // 回车跳到下一个控件
      Enter += this.MySpinEdit_Enter;
      MouseUp += this.MySpinEdit_MouseUp;

      // 显示格式化
      this.Properties.DisplayFormat.FormatString = "{0:0.######}";
      this.Properties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
      this.Properties.EditFormat.FormatString = "{0:0.######}";
      this.Properties.EditFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
      this.Properties.Mask.EditMask = "##################0.######";

      fProperties.DisplayFormat.FormatString = "{0:0.######}";
      fProperties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
      fProperties.EditFormat.FormatString = "{0:0.######}";
      fProperties.EditFormat.FormatType = DevExpress.Utils.FormatType.Numeric;

      // 禁止滚轮
      this.Properties.AllowMouseWheel = false;
    }


    #region fields

    /// <summary>
    /// 
    /// </summary>
    public override string EditorTypeName => RepositoryItemMySpinEdit.EditorName;

    /// <summary>
    /// 
    /// </summary>
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public new RepositoryItemMySpinEdit Properties => base.Properties as RepositoryItemMySpinEdit;

    //
    // 摘要:
    //     Gets or sets whether focus is moved to the next control (according to the tab
    //     order) when an end-user presses ENTER.
    [DefaultValue(true)]
    //[DevExpressXtraEditorsLocalizedDescriptionAttribute("BaseEditEnterMoveNextControl")]
    [DXCategory("Behavior")]
    public override bool EnterMoveNextControl { get; set; } = true;

    #endregion

    /// <summary>
    /// 鼠标移入文本编辑框触发事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>

    private void MySpinEdit_Enter(object sender, EventArgs e) {
      // 设置全选标记
      this.Tag = true;

      var edit = this.Properties.OwnerEdit as TextEdit;
      edit.SelectAll();
    }

    /// <summary>
    /// 获取输入焦点时自动全选
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void MySpinEdit_MouseUp(object sender, MouseEventArgs e) {
      // 如果鼠标左键操作并且标记存在，则执行全选
      if (e.Button == MouseButtons.Left && (bool)this.Tag) {
        var edit = this.Properties.OwnerEdit as TextEdit;
        edit.SelectAll();
      }

      // 取消全选标记
      this.Tag = false;
    }
  }

  /// <summary>
  /// 
  /// </summary>
  [DXCategory("Properties")]
  [UserRepositoryItem("Register")]
  //[ToolboxItem(true)]
  public class RepositoryItemMySpinEdit : RepositoryItemSpinEdit {

    /// <summary>
    /// 
    /// </summary>
    static RepositoryItemMySpinEdit() {
      Register();
    }

    /// <summary>
    /// 
    /// </summary>
    public RepositoryItemMySpinEdit() {
    }

    /// <summary>
    /// 
    /// </summary>
    internal const string EditorName = nameof(MySpinEdit);

    /// <summary>
    /// 
    /// </summary>
    public override string EditorTypeName => EditorName;

    /// <summary>
    /// 
    /// </summary>
    public static void Register() {
      EditorRegistrationInfo.Default.Editors.Add(
        new EditorClassInfo(
          EditorName, // 
          typeof(MySpinEdit),
          typeof(RepositoryItemMySpinEdit),
          typeof(DevExpress.XtraEditors.ViewInfo.BaseSpinEditViewInfo),
          new ButtonEditPainter(),
          true,
          null,
          typeof(BaseSpinEditAccessible)));
    }
  }

}
